﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
namespace BLL
{
    public class OutPatientsBLL
    {
        HospitalDataContext DB= new HospitalDataContext();
        private static int outclinicid=0;
        public string Insert(OutPatientsDTO nhap)
        {
            try
            {
                OutPatient moi=new OutPatient();
                moi.OutClinicID=(outclinicid++).ToString();
                moi.AppointmentID=nhap.AppointmentID;
                moi.StaffID=nhap.StaffID;
                moi.Date=nhap.Date;
                moi.Time=nhap.Time;
                moi.ClinicNo=nhap.ClinicNo;

                DB.OutPatients.InsertOnSubmit(moi);
                DB.SubmitChanges();
                return "success";
            }
            catch(Exception e)
            {
                return e.Message;
            }
        }
        public IQueryable Search(string key)
        {
            var ketqua = (from patient in DB.Patients
                          join appoint in DB.Appointments
                          on patient.PatientID equals appoint.PatientID into paaps
                          from paap in paaps.DefaultIfEmpty()
                          where (patient.Status=="outpa") && (patient.FirstName.Contains(key) || patient.LastName.Contains(key))
                          join outpa in DB.OutPatients
                          on paap.AppointmentID equals outpa.AppointmentID into cuois
                          from cuoi in cuois.DefaultIfEmpty()
                          select new
                          {
                              PatientID = patient.PatientID,
                              FirstName = patient.FirstName,
                              LastName = patient.LastName,
                              Address = patient.Address,
                              Sex = patient.Sex,
                              Tel = patient.Tel,
                              Date = cuoi.Date,
                              Time = cuoi.Time,
                              ClinicNo = cuoi.ClinicNo,
                              StaffID = cuoi.StaffID
                          }).Distinct();
            return ketqua;
        }
        public IQueryable SearchAll()
        {
            var ketqua = (from patient in DB.Patients
                          join appoint in DB.Appointments
                          on patient.PatientID equals appoint.PatientID into paaps
                          from paap in paaps.DefaultIfEmpty()
                          where patient.Status == "outpa"
                          join outpa in DB.OutPatients
                          on paap.AppointmentID equals outpa.AppointmentID into cuois
                          from cuoi in cuois.DefaultIfEmpty()
                          select new
                          {
                              PatientID = patient.PatientID,
                              FirstName = patient.FirstName,
                              LastName = patient.LastName,
                              Address = patient.Address,
                              Sex = patient.Sex,
                              Tel = patient.Tel,
                              Date = cuoi.Date,
                              Time = cuoi.Time,
                              ClinicNo = cuoi.ClinicNo,
                              StaffID = cuoi.StaffID
                          }).Distinct();
            return ketqua;
        } 
    }
}
